import Taro ,{ Component} from '@tarojs/taro'
import { View  , Text} from '@tarojs/components'

import DataManager from '../../utils/DataManager.js'

import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import emptyIcon from '../../image/ic_empty.png'
import BasePage from "../basePage";

import './openAreaList.scss'
import {STORAGE_KEY} from "../../utils/const";
import {base_url} from "../../network/config";

export default class openAreaList extends BasePage{

  config = {
    navigationBarTitleText: '选择地区'
  }

  constructor() {
    super(...arguments)
    this.state = {
      areaList:[],
    }
  }

  componentWillMount(){
  }

  componentDidMount(){
    this.loadAreaList();
  }

  onPullDownRefresh(){
    this.loadAreaList();
  }

  loadAreaList(){
    let _this = this;

    let url = base_url + 'stateless/proxy/proxyList'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
      },
      method: 'GET',
      success(res) {
        let area = [];
        if (res.data.data){
          area = area.concat(res.data.data);
        }
        _this.setState({
          areaList:area,
        })
      }
    })
  }

  chosenArea = (areaData) =>{
    Taro.setStorageSync(STORAGE_KEY.WEIXIN_ID , areaData.weixinId);
    Taro.setStorageSync(STORAGE_KEY.AREA_NAME , areaData.addressName);
    if (Taro.getCurrentPages().length > 1){
      Taro.eventCenter.trigger('areaChange',[]);
    }
    Taro.switchTab({
      url:`/pages/index/index`
    })
  }

  renderAreaItem(areaData){
    return(
      <View className='area-item-container' onClick={this.chosenArea.bind(this ,areaData)}>
        <View className='area-item-main'>
          <Text className='area-item-text'>{areaData.addressName}</Text>
        </View>
        <View className='area-item-divider' />
      </View>
    )
  }

  render(){
    return(
      <View className='area-list-container'>
        {this.state.areaList.length > 0 ? this.state.areaList.map(item => {
            return (
              this.renderAreaItem(item)
            )
          })
          :
          <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon} />
        }
      </View>
    )
  }

}
